/**
 *
 */
package com.glodon.jcloud.increment.shorturl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glodon.jcloud.increment.common.cache.CommonCacheOperator;
import com.glodon.jcloud.increment.shorturl.entities.OAuth2Client;
import com.glodon.jcloud.increment.shorturl.mapper.OAuth2ClientMapper;
import com.glodon.jcloud.increment.shorturl.service.OAuth2ClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import static com.glodon.jcloud.common.constant.RedisObject.CLIENT;

/**
 * Terry MicroService
 *
 * Create Date: 2017年9月13日 下午12:33:09
 *
 * @version: V0.0.1-SNAPSHOT
 * @author: Terry.Li
 * @mail: lib-o@glodon.com
 */
@Slf4j
@Service
public class OAuth2ClientServiceImpl extends ServiceImpl<OAuth2ClientMapper, OAuth2Client> implements OAuth2ClientService, InitializingBean, Serializable {

	private static final long serialVersionUID = 4050220305615731865L;

	@Resource
	protected CommonCacheOperator cache;

	@Resource
	private OAuth2ClientMapper oAuth2ClientMapper;

	/**
	 * @param clientid
	 * @return
	 */
	public OAuth2Client getByClientid(String clientid) {
		OAuth2Client client = (OAuth2Client) cache.get(CLIENT.prefix + clientid);
		if (null == client) {
			client = this.lambdaQuery().eq(OAuth2Client::getClientid, clientid).one();
			cache.put(CLIENT.prefix + clientid, client, CLIENT.expire);
		}
		return client;
	}

	/**
	 * @param clientid
	 * @param clientid
	 * @return
	 */
	public OAuth2Client getByClientidAndClientsecure(String clientid, String clientsecure) {
		List<OAuth2Client> clients = lambdaQuery().eq(OAuth2Client::getClientid, clientid).eq(OAuth2Client::getClientsecure, clientsecure).list();
		return (null != clients && !clients.isEmpty()) ? clients.get(0) : null;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(cache, "[Cache failed] - this argument is required; it must not be null");

		cache.remove(CLIENT.prefix + "MICROSERICE-ALIYUN-P");
		cache.remove(CLIENT.prefix + "CDJG-W");
		cache.remove(CLIENT.prefix + "YNJT-W");
		
	}

}
